package com.css.dzj.zfjg.modules.supervisionreportinfo.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgFbJgjbEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgTsjbReportInfoEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.mapper.JgFbJgjbMapper;
import com.css.dzj.zfjg.modules.supervisionreportinfo.service.JgBlReportService;
import com.css.dzj.zfjg.modules.supervisionreportinfo.service.JgFbReportService;
import com.css.dzj.zfjg.modules.supervisionreportinfo.vo.ReportMsg;
import com.css.dzj.zfjg.modules.supervisionreportinfo.vo.ReportVo;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 *  监管举报办理controller
 * @author xgdou
 */
@RestController
@RequestMapping(value = "/reportbl/")
public class JgBlReportController {

     @Autowired
    JgBlReportService jgBlReportService;
    @Autowired
    private ServiceUtil serviceUtil;
    @Autowired
    BaseService baseService;
    @Autowired
    JgFbJgjbMapper jgFbJgjbMapper;
    @Autowired
    JgFbReportService jgFbReportService;
    /**
     * 根据查询条件查询展示
     * @param pageSize
     * @param pageNo
     * @param tipTerritory
     * @param tipObject
     * @param tipType
     * @param name
     * @param taskState
     * @param fbStartTime
     * @param fbEndTime
     */
    @GetMapping("listReportEntity")
    public R listJgFbEntity(@RequestParam Integer pageSize,
                            @RequestParam Integer pageNo,
                            @RequestParam String tipTerritory,
                            @RequestParam String tipObject,
                            @RequestParam String tipType,
                            @RequestParam String name,
                            @RequestParam String taskState,
                            @RequestParam (required = false) String fbStartTime,
                            @RequestParam (required = false) String fbEndTime
    ) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<ReportMsg> JgFbJgjbEntityQueryResults = jgBlReportService.getReportEntity(pageRequest,tipTerritory,tipObject, tipType, name, taskState,fbStartTime,fbEndTime);
            baseService.logSuccess(3, "多条件分页查询监管举报信息成功", "2");
            return R.ok().put("data", JgFbJgjbEntityQueryResults);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "多条件分页查询监管举报信息失败", "2");
            return R.error(e.getMessage());
        }
    }
    /**
     * 任务查收保存flag 102查收  103拒绝
     */
    @PostMapping("save/{flag}")
    public R save(@RequestBody ReportVo reportVo, HttpServletRequest request, @PathVariable(required = false) String flag) {
        try{
            //将分办信息入库
            JgFbJgjbEntity jgFbJgjbEntity = reportVo.getJgFbJgjbEntity();
            JgFbJgjbEntity oldJgFbJgjbEntity = jgFbReportService.findRegister(jgFbJgjbEntity.getRegisterCode());
            if(oldJgFbJgjbEntity!=null){
                BeanUtil.copyBeanNotEmptyToBean(jgFbJgjbEntity, oldJgFbJgjbEntity);
                jgBlReportService.update(jgFbJgjbEntity,request,flag);
            }else{
                jgFbReportService.add(jgFbJgjbEntity, request);
            }
            //将监管举报信息状态改为已处理
            JgTsjbReportInfoEntity jgTsjbReportInfoEntity = reportVo.getJgTsjbReportInfoEntity();
            jgBlReportService.updatetsjb(jgTsjbReportInfoEntity,request,flag);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "提交失败", "2");
            return R.error("提交失败");
        }
    }

}
